package com.peanut.baby.mvp.livedetail;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.model.ShareType;
import com.peanut.baby.model.VarPoint;
import com.tencent.mars.xlog.Log;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/7/21.
 */

public class LiveDetailPresenter implements LiveDetailContract.Presenter{
    private static final String TAG = LiveDetailPresenter.class.getSimpleName();
    private LiveDetailContract.View view;
    private Activity    activity;
    public LiveDetailPresenter(LiveDetailContract.View view,Activity activity){
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void getRoomDetail(String roomId) {
        Observable observable = RetrofitClient.getInstance().getLiveRoomInfo(InitManager.getInstance().getUserId(),roomId);
        observable.compose(BaseScheduler.<BaseResponse<LiveRoom>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<LiveRoom>() {
            @Override
            protected void onHandleSuccess(LiveRoom o) {
                view.onGetRoomDetailSuccess(o);

            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onRequestFailed(code,msg);
            }

        });
    }

    @Override
    public void joinRoom(String roomId) {
        Observable observable = RetrofitClient.getInstance().enroleLiveRoom(InitManager.getInstance().getUser().userToken,
                InitManager.getInstance().getUserId(),roomId);
        observable.compose(BaseScheduler.<BaseResponse<String>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<String>() {
            @Override
            protected void onHandleSuccess(String o) {
                view.onJoinSuccess();

            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onRequestFailed(code,msg);
            }

        });
    }

    @Override
    public void sharePoint(String roomId) {
        Observable obs = RetrofitClient.getInstance().sharePoint(InitManager.getInstance().getUserId(),
                ShareType.TYPE_SHARE_LIVE+"",roomId);
        obs.compose(BaseScheduler.<VarPoint>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<VarPoint>() {
            @Override
            protected void onHandleSuccess(VarPoint o) {
                InitManager.getInstance().getUser().userPoint = o.totalPoint;
                InitManager.getInstance().updateLocalUser();
                view.onSharePointSuccess("花生壳加"+o.variedPoint+",总花生壳"+o.totalPoint);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                Log.w(TAG,"sharePoint failed "+msg);
//                view.onSharePointSuccess(msg);
            }
        });
    }
}
